package com.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import com.config.StpInterfaceImpl;
import com.dto.ItemSaveDto;
import com.dto.PageDTO;
import com.po.Item;
import com.query.ItemQuery;
import com.result.Result;
import com.service.ItemService;
import com.vo.ItemPageVo;
import com.vo.ItemVo;
import com.vo.UserVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@RestController
@RequestMapping("/item/")
@RequiredArgsConstructor
@Slf4j
public class ItemController {

    private final ItemService itemService;

    /**
     * 上传商品
     * @param name
     * @param image
     * @param price
     * @param stock
     * @param category
     * @param brand
     * @param minuteStatus
     * @param ItemMinuteName
     * @param ItemMinutePrice
     * @param ItemMinuteStatus
     * @param ItemMinuteImage
     * @return
     */
    @SaCheckPermission("user::admin")
    @PostMapping("save")
    public Result save(@RequestParam String name , @RequestParam List<MultipartFile> image,
                       @RequestParam Integer price,@RequestParam Integer stock,
                       @RequestParam String category,@RequestParam String brand,
                       @RequestParam  Integer minuteStatus,@RequestParam(required = false) List<String> ItemMinuteName,
                       @RequestParam(required = false) List<Integer>ItemMinutePrice,@RequestParam(required = false) List<Integer>ItemMinuteStatus,
                       @RequestParam(required = false) List<MultipartFile>ItemMinuteImage,@RequestParam(required = false) String spec){
        ItemSaveDto itemSaveDto = itemService.getitemsavedto(name, image, price, stock, category, brand, minuteStatus,
                ItemMinuteName, ItemMinutePrice, ItemMinuteStatus, ItemMinuteImage,spec);
        log.info("商品上传：{}",itemSaveDto.getImage());
        itemService.saveItems(itemSaveDto);
        return Result.success();
    }

    /**
     * 商品状态修改 1-上架，2-下架，3-删除
     * @param id
     * @return
     */
//    @SaCheckPermission("user::admin")
    @PutMapping("status")
    public Result status(@RequestParam Long id,String status)
    {
        log.info("下架商品：{},{}",id,status);
        StpUtil.checkPermissionOr("user::master","user::admin");
        itemService.iupdate(id,status);
        return Result.success();
    }

    /**
     * 查询商品详细信息
     * @param id
     * @return
     */
    @GetMapping("get")
    public Result<ItemVo> get(@RequestParam Long id){
        log.info("查询商品：{}",id);
        ItemVo itemVo = itemService.get(id);
        return Result.success(itemVo);
    }

    /**
     * 分页查询
     * @param itemQuery
     * @return
     */
    @GetMapping("page")
    public Result<PageDTO<ItemPageVo>> page(ItemQuery itemQuery){
        log.info("分页查询商品：{}",itemQuery);
        PageDTO<ItemPageVo> pageVoPageDTO = itemService.ipage(itemQuery);
        return Result.success(pageVoPageDTO);
    }

    /**
     * 根据商品id商品信息更新
     * @param name
     * @param image
     * @param price
     * @param stock
     * @param category
     * @param brand
     * @param minuteStatus
     * @param ItemMinuteName
     * @param ItemMinutePrice
     * @param ItemMinuteStatus
     * @param ItemMinuteImage
     * @param spec
     * @return
     */
    @SaCheckPermission("user::admin")
    @PutMapping("put")
    public Result put(@RequestParam Long id,@RequestParam(required = false) String name , @RequestParam(required = false) List<MultipartFile> image,
                      @RequestParam(required = false) Integer price,@RequestParam(required = false) Integer stock,
                      @RequestParam(required = false) String category,@RequestParam(required = false) String brand,
                      @RequestParam(required = false)  Integer minuteStatus,@RequestParam(required = false) List<String> ItemMinuteName,
                      @RequestParam(required = false) List<Integer>ItemMinutePrice,@RequestParam(required = false) List<Integer>ItemMinuteStatus,
                      @RequestParam(required = false) List<MultipartFile>ItemMinuteImage,@RequestParam(required = false) String spec){
        ItemSaveDto itemSaveDto = itemService.getitemsavedto(name, image, price, stock, category, brand, minuteStatus,
                ItemMinuteName, ItemMinutePrice, ItemMinuteStatus, ItemMinuteImage,spec);
        log.info("根据商品id商品信息更新:{}",id);
        itemService.put(id,itemSaveDto);
        return Result.success();
    }

    /**
     * 根据商品id获取商品状态
     * @param id
     * @return
     */
    @GetMapping("status")
    public Result<Integer> integerResult(@RequestParam Long id){
        log.info("根据商品id获取商品状态:{}",id);
        Item item = itemService.getById(id);
        return Result.success(item.getStatus());
    }

    /**
     * 根据商品id查询购买者
     * @param id
     * @return
     */
    @SaCheckPermission("user::admin")
    @GetMapping("userlist")
    public Result<List<UserVo>> userList(@RequestParam Long id){
        log.info("根据商品id查询购买者:{}",id);
        List<UserVo> userVoList = itemService.userList(id);
        return Result.success(userVoList);
    }
}
